package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
    //保存基本信息 到 setmeal数据库
    this.save(setmealDto);
    //保存每个SetmealId的值
    List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
    setmealDishes.forEach(item ->item.setSetmealId(setmealDto.getId()));
    //保存到setmeal_dish数据库
    setmealDishService.saveBatch(setmealDishes);
    }
    //分页查询
    @Override
    public R<Page<SetmealDto>> pageWithDish(Integer page, Integer pageSize, String name) {
        Page<Setmeal> pageInfo1 = new Page<>(page,pageSize);
        //设置条件构造器
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),"name",name);
        queryWrapper.orderByDesc("update_time");
        setmealService.page(pageInfo1,queryWrapper);
        //开始进行对象拷贝
        Page<SetmealDto> pageInfo2 = new Page<>(page,pageSize);
        //创建SetmealDto对象集合 用来存储SetmealDto数据
        List<SetmealDto> setmealDtoslist = new ArrayList<>();
        for (Setmeal setmeal : pageInfo1.getRecords()) {
            SetmealDto setmealDto = new SetmealDto();
            //将setmeal数据copy到setmealDto内
            BeanUtils.copyProperties(setmeal,setmealDto);
            //根据分类id查询分类对象
            Category category = categoryService.getById(setmeal.getCategoryId());
            if(category!=null){
                //设置分类名称
                setmealDto.setCategoryName(category.getName());
                //将setmealDto添加到list集合中
                setmealDtoslist.add(setmealDto);
            }
        }
        //将setmealDtoslist集合封装到pageInfo2中
        pageInfo2.setRecords(setmealDtoslist);
        //拷贝第二部分数据   pageInfo2已经有了records数据 所以不用拷贝这一部分数据
        BeanUtils.copyProperties(pageInfo1,pageInfo2,"records");
        return R.success(pageInfo2);
    }
    //删除
    @Override
    public void deleteWithDish(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        //查询套餐状态 确定是否可用删除
        QueryWrapper<Setmeal> queryWrapper1= new QueryWrapper<>();
        queryWrapper1.eq(ids.length>0,"status",1);
        queryWrapper1.in("id",list);
        int count = this.count(queryWrapper1);
        if(count>0){
            throw new CustomerException("套餐正在售卖，不能删除！");
        }
        setmealService.removeByIds(list);
        QueryWrapper<SetmealDish> queryWrapper2  = new QueryWrapper<>();
        queryWrapper2.in("setmeal_id",list);
        setmealDishService.remove(queryWrapper2);
    }
    //启用和停用
    @Override
    public void updateStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            this.updateById(setmeal);
        }
    }
    //修改信息
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //现根据id查询信息
        this.updateById(setmealDto);
        //创建条件构造器  清理菜品数据 setmeal_dish表的delete操作
        QueryWrapper<SetmealDish> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",setmealDto.getId());
        //删除菜品信息
        setmealDishService.remove(queryWrapper);
        //添加新的菜品信息
        List<SetmealDish> setmealDisheslist = setmealDto.getSetmealDishes();
        //添加到表中
        setmealDisheslist.forEach(item -> item.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDisheslist);
    }
    //数据回显
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询基本信息
        Setmeal setmeal = this.getById(id);
        //创建条件构造器 查询关于套餐下的菜品信息 为list集合
        QueryWrapper<SetmealDish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //创建SetmealDto
        SetmealDto setmealDto = new SetmealDto();
        //导入数据
        BeanUtils.copyProperties(setmeal,setmealDto);
        //基本数据导入完成之后还需要导入相关的菜品数据
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

}
